package 面试代码.线程顺序打印面试版;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 阻塞队列同样也可以用来进行线程调度。
 * 利用队列的长度，来确定执行者
 * 利用队列的阻塞性，来保证入队操作同步执行。
 */
public class Test3 {
    private static BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                System.out.println("A");
                queue.offer("B");
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                while (queue.size() != 1) {
                }
                System.out.println("B");
                queue.offer("C");
            }
        };
        Thread t3 = new Thread() {
            @Override
            public void run() {
                while (queue.size() != 2) {
                }
                System.out.println("C");
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }
}
